package serverManger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import message.FileWasChanged;
import message.Message;
import message.MessageType;
/**
 * handling the transfer of messages to clients
 * @author ran
 *
 */
public class CompMessages {
	HashMap<String,Message> filesList;
	ArrayList<Message> queueOfMessage;
	/**
	 * constructor of class
	 */
	public CompMessages() {
		filesList=new HashMap<String, Message>();
		queueOfMessage = new ArrayList<Message>();
	}
	/**
	 * 
	 * @return the next message in message queue
	 */
	public Message getNextMessage(){
		if(!queueOfMessage.isEmpty())
			return queueOfMessage.remove(0);
		if(!filesList.isEmpty()){
			Set<String> set =  filesList.keySet();
			String first = set.iterator().next();
			return filesList.remove(first);
		}
		return null;
	}
	/**
	 * add message to queue
	 * @param message message to add
	 */
	public void pushMessage(Message message){
		if(message.getType()==MessageType.FILE_WAS_CHANGED){
			FileWasChanged fileWasChanged = (FileWasChanged)message;
			filesList.put(fileWasChanged.getBoxName()+"/"+fileWasChanged.getFileName(), fileWasChanged);
		}
		else
			queueOfMessage.add(message);
	
	}
	/**
	 * 
	 * @return the amount of messages that wait to the client
	 */
	public int getSize(){
		return filesList.size()+queueOfMessage.size();
	}
}
